package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.PageParams;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;

    @CacheEvict(cacheNames = "setMealCache" ,allEntries = true)
    @PostMapping
    public R save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }

    @GetMapping("/page")
    public R findPage(PageParams pageParams) {
        Page page = setmealService.findPage(pageParams);
        return R.success(page);
    }
  @CacheEvict(cacheNames = "setMealCache" ,allEntries = true)
    @DeleteMapping
    public R deleteSetMeal(@RequestParam List<Long> ids) {
        System.out.println(1111);
        log.info("ids{},", ids);
        int rows = setmealService.deleteSetmeal(ids);
        if (rows > 0) {
            return R.success("删除成功");
        } else {
            return R.error("删除成功");
        }

    }

    /**
     * 根据分类id 和状态查询数据，缓存到redis中
     * @Cacheable中是先查询缓存，没有再查询数据库。
     * 根据条件查询套餐数据
     */
    @Cacheable(key = "#setmeal.categoryId+'_'+#setmeal.status", cacheNames = "setMealCache")
    @GetMapping("/list")
    public R list(Setmeal setmeal) {
        try {
            List<Setmeal> list = setmealService.list(setmeal);
            return R.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询失败");
        }
    }

    /**
     * 更新套餐
     *
     * @param setmealDto 前端更新页面传回来的数据
     * @return
     */
    @PutMapping
    public R updateSetmeal(@RequestBody SetmealDto setmealDto) {
        int row = setmealService.updatSetmeal(setmealDto);
        if (row > 0) {
            return R.success("修改成功");
        } else {
            return R.error("修改失败");
        }

    }

    @PostMapping("/status/{status}")
    public R updateSetmealStatus(@RequestParam List<Long> ids, @PathVariable Integer status) {
        int rows = setmealService.updatSetmealStatus(ids, status);
        if (rows > 0) {
            return R.success("修改状态成功");
        } else {
            return R.error("修改状态失败");
        }

    }

    @GetMapping("/dish/{id}")
    public R findDishBySemtmealId(@PathVariable Long id) {
         SetmealDto setmealDto = setmealService.fiandSetmealDto(id);
        //Setmeal setmeal = setmealService.findSetmealById(id);
       // System.out.println(setmeal.getImage());
        return R.success(setmealDto);
    }
}
